/*
 * TextComponentDelegate.cpp  0.4.0 / Dec 03, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "TextComponentDelegate.h"

// TextComponentDelegate :: Protected Constructor. -----------------------------

/*!
 * \brief Constructs new TextComponentDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the TextComponent java object
 * \param textComponent the text component
 */
TextComponentDelegate::TextComponentDelegate(JNIEnv *env, jobject obj,
                                             QWidget *textComponent)
  : ComponentDelegate(env, obj, textComponent), m_undoAvailable(false),
    m_redoAvailable(false), m_cutAvailable(false), m_copyAvailable(false),
    m_pasteAvailable(false), m_clearAvailable(false),
    m_selectAllAvailable(false)
{
  // empty constructor body
}

// TextComponentDelegate :: Static Native Calls. -------------------------------

/*!
 * \brief Initializes TextComponentDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the TextComponent java class
 *
 * \exception FatalError if failed
 */
void TextComponentDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT (!sm_undoAvailableMID);
  if (!INIT_UMID(undoAvailable, cls, "(Z)V") || 
      !INIT_UMID(redoAvailable, cls, "(Z)V") || 
      !INIT_UMID(cutAvailable, cls, "(Z)V") || 
      !INIT_UMID(copyAvailable, cls, "(Z)V") || 
      !INIT_UMID(pasteAvailable, cls, "(Z)V") || 
      !INIT_UMID(clearAvailable, cls, "(Z)V") ||
      !INIT_UMID(selectAllAvailable, cls, "(Z)V")) {
    FATAL_LOGGED_ERROR("TextComponentDelegate initialization failed");
  }
}

// TextComponentDelegate :: Native Callbacks. ----------------------------------

/*!
 * \brief Calls \c undoAvailable method of the TextDocument object.
 *
 * \param available \c true if available
 */
void TextComponentDelegate::undoAvailable(jboolean available)
{
  if (m_undoAvailable != (bool)available) {
    m_undoAvailable = (bool)available;
    jniObject()->callVoidMethod(sm_undoAvailableMID, available);
  }
}

/*!
 * \brief Calls \c redoAvailable method of the TextDocument object.
 *
 * \param available \c true if available
 */
void TextComponentDelegate::redoAvailable(jboolean available)
{
  if (m_redoAvailable != (bool)available) {
    m_redoAvailable = (bool)available;
    jniObject()->callVoidMethod(sm_redoAvailableMID, available);
  }
}

/*!
 * \brief Calls \c cutAvailable method of the TextDocument object.
 *
 * \param available \c true if available
 */
void TextComponentDelegate::cutAvailable(jboolean available)
{
  if (m_cutAvailable != (bool)available) {
    m_cutAvailable = (bool)available;
    jniObject()->callVoidMethod(sm_cutAvailableMID, available);
  }
}

/*!
 * \brief Calls \c copyAvailable method of the TextDocument object.
 *
 * \param available \c true if available
 */
void TextComponentDelegate::copyAvailable(jboolean available)
{
  if (m_copyAvailable != (bool)available) {
    m_copyAvailable = (bool)available;
    jniObject()->callVoidMethod(sm_copyAvailableMID, available);
  }
}

/*!
 * \brief Calls \c pasteAvailable method of the TextDocument object.
 *
 * \param available \c true if available
 */
void TextComponentDelegate::pasteAvailable(jboolean available)
{
  if (m_pasteAvailable != (bool)available) {
    m_pasteAvailable = (bool)available;
    jniObject()->callVoidMethod(sm_pasteAvailableMID, available);
  }
}

/*!
 * \brief Calls \c clearAvailable method of the TextDocument object.
 *
 * \param available \c true if available
 */
void TextComponentDelegate::clearAvailable(jboolean available)
{
  if (m_clearAvailable != (bool)available) {
    m_clearAvailable = (bool)available;
    jniObject()->callVoidMethod(sm_clearAvailableMID, available);
  }
}

/*!
 * \brief Calls \c selectAllAvailable method of the TextDocument object.
 *
 * \param available \c true if available
 */
void TextComponentDelegate::selectAllAvailable(jboolean available)
{
  if (m_selectAllAvailable != (bool)available) {
    m_selectAllAvailable = (bool)available;
    jniObject()->callVoidMethod(sm_selectAllAvailableMID, available);
  }
}

// TextComponentDelegate :: Protected Member Functions. ------------------------

/*!
 * \brief \c undoAvailable slot.
 *
 * \param available \c true if available
 */
void TextComponentDelegate::undoAvailableSlot(bool available)
{
  undoAvailable(available);
}

/*!
 * \brief \c redAvailable slot.
 *
 * \param available \c true if available
 */
void TextComponentDelegate::redoAvailableSlot(bool available)
{
  redoAvailable(available);
}

/*!
 * \brief Clipboard \c dataChanged slot.
 */
void TextComponentDelegate::clipboardDataChanged()
{
  pasteAvailable(!QApplication::clipboard()->text().isEmpty());
}

// TextComponentDelegate :: Private Static Data Members. -----------------------

jmethodID TextComponentDelegate::sm_undoAvailableMID = 0;
jmethodID TextComponentDelegate::sm_redoAvailableMID = 0;
jmethodID TextComponentDelegate::sm_cutAvailableMID = 0;
jmethodID TextComponentDelegate::sm_copyAvailableMID = 0;
jmethodID TextComponentDelegate::sm_pasteAvailableMID = 0;
jmethodID TextComponentDelegate::sm_clearAvailableMID = 0;
jmethodID TextComponentDelegate::sm_selectAllAvailableMID = 0;
